<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use catetree\Catetree;
class Category extends Controller
{
	public function lst()
	{	
		//商品分类栏目的排序
		$cate=new Catetree();
		$categoryObj=db('category');
		if(request()->isPost())
		{
			$data=input('post.');
			$result=$cate->cateSort($data['sort'],$categoryObj);
			if($result)
			{
				$this->success('排序成功',url('lst'));
			}else
			{
				$this->error('排序失败');
			}
		}
		$categoryRes=$categoryObj->order('sort desc')->select();
		$categoryRes=$cate->catetree($categoryRes);
		$settitle=settitle('商品分类');
		$this->assign(
			array(
				'settitle'=>$settitle,
				'categoryRes'=>$categoryRes
			)
		);
		return view();
	}
	public function add()
	{		
		$cate=new Catetree();
		$categoryObj=db('category');
		if(request()->isPost())
		{
			$data=input('post.');

			//处理上传图片
			if($_FILES['cate_img']['tmp_name'])
			{
				$imgname='cate_img';
				$filename=IMG_UPLOADS.DS.'category';
				$data['cate_img']=uploadsingle($imgname,$filename);
			}
			//验证
/*    		$validate = validate('Cate');
    		if(!$validate->check($data)){
			    $this->error($validate->getError());
			}*/
			$add=$categoryObj->insert($data);
			if($add)
			{
				$this->success('添加商品分类成功',url('lst'));
			}else
			{
				$this->error('添加商品分类失败');
			}
		}
		$settitle=settitle('商品分类');
		$categoryRes=$categoryObj->order('sort desc')->select();
		$categoryRes=$cate->catetree($categoryRes);
		$this->assign(
			array(
				'settitle'=>$settitle,
				'categoryRes'=>$categoryRes
			)
		);
		return view();
	}

	public function edit()
	{	
		$id=input('id');
		$cate=new Catetree();
		$categoryObj=db('category');
		if(request()->isPost())
		{
			$data=input('post.');

			//处理上传图片
			if($_FILES['cate_img']['tmp_name'])
			{
				$fieldsname='cate_img';
				$dir=IMG_UPLOADS;
				$catename='category';
				$result=delimg($id,$categoryObj,$fieldsname,$dir,$catename);

				$imgname='cate_img';
				$filename=$dir.DS.$catename;
				$data['cate_img']=uploadsingle($fieldsname,$filename);
			}	
			//验证
/*    		$validate = validate('Cate');
    		if(!$validate->check($data)){
			    $this->error($validate->getError());
			}*/
			$save=$categoryObj->update($data);
			if($save!=false)
			{
				$this->success('修改商品分类成功',url('lst'));
			}else
			{
				$this->error('修改商品分类失败');
			}
		}
		$categorys=$categoryObj->find($id);
		$settitle=settitle('商品分类');
		$categoryRes=$categoryObj->order('sort desc')->select();
		$categoryRes=$cate->catetree($categoryRes);
		$this->assign(
			array(
				'categorys'=>$categorys,
				'settitle'=>$settitle,
				'categoryRes'=>$categoryRes
			)
		);
		return view();
	}

	public function del($id)
	{	
		//先查询当前商品分类的子商品分类
		$category=db('category');
		$cateTree=new Catetree();
		$sonids=$cateTree->childrenids($id,$category);
		$sonids[]=intval($id);

		
        foreach ($sonids as $k => $v) {
         //删除商品分类前判断该商品分类下的相关缩略图
			$fieldsname='cate_img';
			$dir=IMG_UPLOADS;
			$catename='category';
			$result=delimg($v,$category,$fieldsname,$dir,$catename);
			$del=$category->delete($v);
        }
		
		if($del)
		{
			$this->success('删除商品分类成功',url('lst'));
		}else
		{
			$this->error('删除商品分类失败');
		}
	}


	//ajax修改分类启用状态
	public function ajaxchangestatus()
	{
		$id=input('post.id');
		$obj=db('category');
		$status='show_cate';
		$result=ajaxchangestatus($id,$obj,$status);
		echo $result;
	}





}
